//
//  OperationView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/15.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//


//operat   idle
//truck  chargeCar  other
//total

//All state

#import "OperationView.h"
#import "OperationTotalCell.h"
#import "OperationOnlineCell.h"
#import "OperationUsedCell.h"
#import "OperatableStatuCell.h"
#import "PieOperationCell.h"


@interface OperationView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *cavTableView;

@property (nonatomic,strong)NSDictionary *carNumDic;
@property (nonatomic,strong)NSArray *carUseInStatis;
@property (nonatomic,strong)NSArray *carUseStatis;
@property (nonatomic,assign)BOOL allowLoad;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end

@implementation OperationView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
        _allowLoad = NO;
        _needRefresh = YES;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setUpUI
{
    _cavTableView = [UITableView new];
    _cavTableView.dataSource = self;
    _cavTableView.delegate = self;
    _cavTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _cavTableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    self.cavTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self addSubview:self.cavTableView];
    [self.cavTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight-10);
    }];
}
- (void)loadNewData
{
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_OperatingStateByDay method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            NSDictionary *dic = responseObject[@"obj"];
            self.carNumDic = dic[@"carNum"];
            self.carUseInStatis = dic[@"carUseInStatis"];
            self.carUseStatis = dic[@"carUseStatis"];
            _allowLoad = YES;
        }
        [_cavTableView.mj_header endRefreshing];
        [_cavTableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
        [_cavTableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allowLoad) {
        return 5;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            OperationTotalCell *totalcell = [OperationTotalCell new];
            if (_carNumDic) {
                totalcell.total = [NSString stringWithFormat:@"%@",_carNumDic[@"totalCarNum"]];
            }
            totalcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return totalcell;
        }
            break;
        case 1:
        {
            OperationOnlineCell *onlinecell = [OperationOnlineCell new];
            onlinecell.leftTitle = @"上线数";
            onlinecell.rightTitle = @"上线率";
            if (_carNumDic) {
                
                int totalNum = [_carNumDic[@"totalCarNum"] intValue];
                int useNum = [_carNumDic[@"onlineCarNum"] intValue];
                NSString *str = @"%";
                double percent;
                if (totalNum == 0) {
                    percent = 0;
                }else{
                    percent = (useNum*1.00f)/(totalNum*1.00f);
                }
                onlinecell.leftData = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",useNum]];
                 onlinecell.rightData = [NSString stringWithFormat:@"%.2lf%@",percent*100,str];
            }
            onlinecell.dic = _carNumDic;
            onlinecell.selectionStyle = UITableViewCellSelectionStyleNone;
            return onlinecell;
        }
            break;
        case 2:
        {
            OperationUsedCell *usedcell = [OperationUsedCell new];
            usedcell.dic = _carNumDic;
            usedcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return usedcell;
        }
            break;
        case 3:
        {
            OperatableStatuCell *tablecell = [OperatableStatuCell new];
            if (self.carUseInStatis.count) {
                [tablecell laodUIWithDataArr:self.carUseInStatis];
            }
            tablecell.selectionStyle = UITableViewCellSelectionStyleNone;
            return tablecell;
        }
            break;
        case 4:
        {
            PieOperationCell *piecell = [PieOperationCell new];
            piecell.title = @"运营状态";
            if (self.carUseStatis.count) {
                [piecell laodUIWithDataArr:self.carUseStatis];
            }
            piecell.selectionStyle = UITableViewCellSelectionStyleNone;
            return piecell;
        }
            break;
        default:
            return nil;
            break;
    }
    

}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 94;
            break;
        case 1:
            return 109;
            break;
        case 2:
            return 129;
            break;
        case 3:
            return 281+(self.carUseInStatis.count-3)*40;
            break;
        case 4:
            return 299;
            break;
        default:
            return 299;
            break;
    }
}

- (void)loadDataWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_OperatingStateByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dic = responseObject[@"obj"];
                self.carNumDic = dic[@"carNum"];
                self.carUseInStatis = dic[@"carUseInStatis"];
                self.carUseStatis = dic[@"carUseStatis"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
            [_cavTableView reloadData];
             [_cavTableView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
             [_cavTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
    
    
}


- (void)loadDataBackGroundWithParam:(NSDictionary *)param
{
   
    if (_needRefresh) {
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_OperatingStateByDay method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
             [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dic = responseObject[@"obj"];
                self.carNumDic = dic[@"carNum"];
                self.carUseInStatis = dic[@"carUseInStatis"];
                self.carUseStatis = dic[@"carUseStatis"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
            [_cavTableView reloadData];
             [_cavTableView.mj_header endRefreshing];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
             [_cavTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
    
    
}

@end
